package com.liefox.serviceedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liefox.commonutils.exceptionhandler.GuliException;
import com.liefox.serviceedu.entity.EduChapter;
import com.liefox.serviceedu.entity.EduVideo;
import com.liefox.serviceedu.entity.chapter.ChapterVo;
import com.liefox.serviceedu.entity.chapter.VideoVo;
import com.liefox.serviceedu.mapper.EduChapterMapper;
import com.liefox.serviceedu.service.EduChapterService;
import com.liefox.serviceedu.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author liefox
 * @since 2021-07-14
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;

    @Override
    public List<ChapterVo> getAllOneTwoSubject(String courseId) {
        //根据课程id查询所属章节
        final QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id", courseId).orderByDesc("sort");
        final List<EduChapter> eduChaptersList = baseMapper.selectList(wrapperChapter);

        //根据课程id查询所属小节
        final QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id", courseId).orderByDesc("sort");
        List<EduVideo> eduVideoList = videoService.list(wrapperVideo);

        //创建一个最终的集合，用于封装数据
        List<ChapterVo> finalList = new ArrayList<>();

        //遍历查询章节list集合
        for (int i = 0; i < eduChaptersList.size(); i++) {
            final EduChapter eduChapter = eduChaptersList.get(i);
            //eduChapter对象复制到ChapterVo里面
            final ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter, chapterVo);
            //把chapterVo放到最终list集合
            finalList.add(chapterVo);

            //封装小节
            final ArrayList<VideoVo> finalChapterVidoe = new ArrayList<>();
            //遍历小节
            for (int m = 0; m < eduVideoList.size(); m++) {
                final EduVideo eduVideo = eduVideoList.get(m);
                if (eduVideo.getChapterId().equals(eduChapter.getId())) {

                    //把eduVideo值复制到videoVo里面，放到finalChapterVidoe
                    final VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo, videoVo);
                    finalChapterVidoe.add(videoVo);
                }
            }
            //把一级下面所有二级分类放到一级分类里面
            chapterVo.setChildren(finalChapterVidoe);
        }
        return finalList;
    }

    @Override
    public Boolean delChapter(String chapterId) {
        final QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        final int count = videoService.count(wrapper);
        if (count > 0) {
            throw new GuliException(20001, "不能删除,必须先删除该章节的其他小节！");
        } else {
            final int i = baseMapper.deleteById(chapterId);
            return i > 0;
        }
    }

    @Override
    public void removeChapterByCourseId(String courseId) {
        final QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        baseMapper.delete(wrapper);

    }

}
